package editeur;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.*;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("unused")
public class Album implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 965264620660034601L;
	String nom;
	boolean saved=false;
	private Vector<Photo> album=new Vector<Photo>();
	private JButton jb;
	
	public Album(){
	}
	
	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public boolean isSaved() {
		return saved;
	}

	public void setSaved(boolean saved) {
		this.saved = saved;
	}

	public Vector<Photo> getAlbum() {
		return album;
	}

	public void setAlbum(Vector<Photo> album) {
		this.album = album;
	}
	
	public boolean AddPhoto( Photo p){
		if(album.contains(p))
		{
			JOptionPane.showMessageDialog(null,"Photo déjà présente");
			return false;
		}
		album.add(p);
		JOptionPane.showMessageDialog(null,"Photo ajoutée");
		return true;
		
	
	}
	
	public boolean RemovePhoto(Photo p){
		if(album.contains(p))
		{
			JOptionPane.showMessageDialog(null,"Photo supprimée");
			return album.remove(p);
		}
		else
		{
			JOptionPane.showMessageDialog(null,"Photo Inexistante");
			return false;
		}
			
	}
	
	public boolean  Save(JFrame f) throws IOException{
		if(this.saved==true)
		{
			JOptionPane.showMessageDialog(null,"Fichier à jour");
			return saved;
		}
		int confirm=0;
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        File file=null;
        do{
        	int returnVal = fc.showSaveDialog(f);
            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
            	file = fc.getSelectedFile();
                
                if(file.exists())
                {
                	confirm=JOptionPane.showConfirmDialog(null, "Ce fichier existe. Voulez vous l'écraser");
                	if(confirm==JOptionPane.YES_OPTION)
                	{              	
                		file.mkdir();
                		for(Photo p:album){
                		ImageIO.write(p,"jpg",new File(file.getAbsolutePath()+System.getProperty("file.separator")+p.getNom()));
                		}
                		saved=true;
                		JOptionPane.showMessageDialog(null, "Fichier bien sauvegardé");
                	
                	}   
                
                }
                else{
                	file.mkdir();
            		for(Photo p:album){
            		ImageIO.write(p,"jpg",new File(file.getAbsolutePath()+System.getProperty("file.separator")+p.getNom()));
            		}
            		saved=true;
            		JOptionPane.showMessageDialog(null, "Fichier bien sauvegardé");
                }
            }
            else if(returnVal==JFileChooser.CANCEL_OPTION)
            {
            	return false;
            }
           }while(saved==false);
            return saved;
     
        
		
	}
	
	public void afficherContenu(){
		for(Photo p : album){
			System.out.println(p.getNom());
		
		}
	}
	
	

	public Album Load(JFrame j) throws Exception
	{
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        File file=null;
        BufferedImage bimg;
        int returnVal=fc.showOpenDialog(j);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
        	file = fc.getSelectedFile();
            if(file.exists())
            {
            	Album alb=new Album();
            	alb.setNom(file.getName());
            	File[] images=null;
            	images=file.listFiles();
            	for(File f:images){
            		bimg=ImageIO.read(f);
            		alb.getAlbum().add(new Photo(f.getName(),bimg));
            		
            	}
            	return alb;
            }
            else
            {
            	System.out.println("Cet album n'existe pas");
            	return null;
            }
        
        }
        else if(returnVal==JFileChooser.CANCEL_OPTION)
        {
        	return null;
        }
        else
        	return null;
	}
	
	public static void main(String args[]) throws Exception{
		JFrame j=new JFrame();
		j.setSize(800, 600);
		j.setLocationRelativeTo(null);
		j.setResizable(false);
		j.setTitle("Test");
		j.setVisible(true);
		j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		j.setForeground(Color.BLACK);
		Graphics2D g=(Graphics2D)j.getGraphics();	
		/*String ref="";
	        JFileChooser fc = new JFileChooser();
	        File file=null;
	        int returnVal=fc.showOpenDialog(null);
	        if (returnVal == JFileChooser.APPROVE_OPTION)
	        {
	        	file = fc.getSelectedFile();
	        	ref=file.getAbsolutePath();
	        }
		BufferedImage img=ImageIO.read(new File(ref));
		Photo p=new Photo("p",img);*/
		//Photo p2=new Photo("p2");
		//Photo p3=new Photo("p3");
		
		
	
		Album a=new Album();
		//a.AddPhoto(p);
		//a.AddPhoto(p3);
		//a.AddPhoto(p2);
		//a.Save(j);
		a.Load(j);
		g.drawImage(a.album.elementAt(0),null,0,0);
		a.afficherContenu();
		//a.RemovePhoto(p);
		
	}
	

	

}
